<template>
  <div class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="admin-sidebar" :style="{ width: isCollapse ? '64px' : '200px' }">
      <div class="logo-container">
        <img src="/logo.svg" alt="Logo" class="logo">
        <span v-show="!isCollapse" class="title">后台管理</span>
      </div>
      <el-menu
        :default-active="$route.path"
        class="admin-menu"
        :router="true"
        :collapse="isCollapse"
      >
        <el-menu-item index="/admin">
          <el-icon><Monitor /></el-icon>
          <template #title>数据统计</template>
        </el-menu-item>

        <el-sub-menu index="/admin/content">
          <template #title>
            <el-icon><Document /></el-icon>
            <span v-show="!isCollapse">内容管理</span>
          </template>
          <el-menu-item index="/admin/videos">
            <el-icon><VideoCamera /></el-icon>
            <span>视频管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/posts">
            <el-icon><Reading /></el-icon>
            <span>帖子管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/comments">
            <el-icon><ChatDotRound /></el-icon>
            <span>评论管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/categories">
            <el-icon><Files /></el-icon>
            <span>分类管理</span>
          </el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/admin/users">
          <el-icon><User /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>

        <el-menu-item index="/admin/settings">
          <el-icon><Setting /></el-icon>
          <template #title>系统设置</template>
        </el-menu-item>
        <el-menu-item @click="handleBackToSite">
          <el-icon><Back /></el-icon>
          <template #title>返回网站</template>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主要内容区域 -->
    <el-container class="admin-container">
      <!-- 使用通用的 TheHeader 组件 -->
      <TheHeader @toggle-sidebar="toggleSidebar" />

      <!-- 内容区域 -->
      <el-main class="admin-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
// import { useStore } from 'vuex' // Store is likely used by TheHeader
import TheHeader from '@/components/TheHeader.vue'; // 导入通用 Header
import {
  Monitor,
  Document,
  VideoCamera,
  Reading,
  ChatDotRound,
  Files,
  User,
  Setting,
  Back
  // Icons related to the old header are removed
} from '@element-plus/icons-vue'
// import { ElMessageBox } from 'element-plus' // MessageBox is likely handled by TheHeader

const router = useRouter()
// const route = useRoute() // route might not be needed if currentPath is removed
// const store = useStore() // store might not be needed directly here

// 侧边栏折叠状态 - 需要保留并可能由 TheHeader 控制
const isCollapse = ref(false)

// 移除原顶部栏相关的计算属性和方法
// const currentUser = computed(() => store.getters['auth/user']);
// const userAvatar = computed(() => { ... });
// const userName = computed(() => currentUser.value?.username || '管理员')
// const currentPath = computed(() => { ... });
// const handleCommand = async (command) => { ... };

// 切换侧边栏 - 由 TheHeader 触发
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 处理返回网站 - 保留
const handleBackToSite = () => {
  router.push('/')
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  display: flex;
}

.admin-sidebar {
  /* height: 100vh; // Redundant with admin-layout */
  background-color: #304156;
  transition: width 0.3s;
  overflow-x: hidden;
  flex-shrink: 0; /* Prevent sidebar from shrinking */
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center; /* Center items when collapsed */
  padding: 0 16px;
  background-color: #2b3649;
  overflow: hidden; /* Hide title when collapsing */
}

.logo {
  height: 32px;
  margin-right: 12px;
  flex-shrink: 0;
}

.title {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
}

.admin-menu {
  border-right: none;
  background-color: transparent;
  height: calc(100% - 60px); /* Fill remaining height */
  overflow-y: auto; /* Add scroll if menu is long */
}

/* Adjust menu item padding when collapsed */
.admin-menu:not(.el-menu--collapse) .el-sub-menu__title span,
.admin-menu:not(.el-menu--collapse) .el-menu-item span {
  display: inline-block;
}

.admin-menu.el-menu--collapse .el-sub-menu__title span,
.admin-menu.el-menu--collapse .el-menu-item span {
  /* display: none; // Hiding span might cause issues, let Element Plus handle it */
  height: 0;
  width: 0;
  overflow: hidden;
  visibility: hidden;
  display: inline-block;
}
.admin-menu.el-menu--collapse .el-sub-menu__title .el-icon,
.admin-menu.el-menu--collapse .el-menu-item .el-icon {
  margin: 0;
}


.admin-menu :deep(.el-menu-item),
.admin-menu :deep(.el-sub-menu__title) {
  color: #bfcbd9;
}

.admin-menu :deep(.el-menu-item:hover),
.admin-menu :deep(.el-sub-menu__title:hover) {
  background-color: #263445;
}

.admin-menu :deep(.el-menu-item.is-active) {
  color: #409eff;
  background-color: #263445;
}

.admin-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Remove styles related to the old admin-header */
/* .admin-header { ... } */
/* .header-left { ... } */
/* .collapse-btn { ... } */
/* .header-right { ... } */
/* .admin-avatar { ... } */
/* .admin-name { ... } */

.admin-main {
  background-color: #f0f2f5;
  /* height: calc(100vh - 60px); // Height is now determined by flex */
  flex-grow: 1; /* Allow main content to grow */
  overflow-y: auto;
  padding: 20px;
}

/* 路由切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>